package cn.sh.system.controller;

import cn.sh.org.domain.Department;
import cn.sh.org.domain.Employee;
import cn.sh.system.service.IMenuService;
import cn.sh.system.domain.Menu;
import cn.sh.system.query.MenuQuery;
import cn.sh.basic.util.AjaxResult;
import cn.sh.basic.page.PageList;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    /*新增和修改*/
    @PutMapping
    public AjaxResult addOneUpdateMenu(@RequestBody Menu menu){
        System.out.println(menu);
        try {
            if(menu.getId() == null){
                //新增
                menuService.add(menu);
            }else {
                //修改
                menuService.update(menu);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("操作失败");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                menuService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value =  "通过ID查询")
    public Menu get(@ApiParam(name="ID属性",required = true) @PathVariable("id") Long id)
    {
        return menuService.selectOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Menu> list(){

        return menuService.selectAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Menu> json(@RequestBody MenuQuery query)
    {
        return menuService.queryData(query);
    }

    /*获取部门树状结构*/
    @GetMapping("/getTree")
    public List<Menu> getTree(){
        List<Menu> tree = menuService.getTree();
        return tree;
    }

    /*批量删除*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("操作失败");
        }
    }
}